parseURL

Parse the input string as a URL.

pure @safe
parseURL
(
string value
)

Throws

URLException if the string was in an incorrect format.

Examples

1 t {
2 	{
3 		// Infer scheme
4 		auto u1 = parseURL("example.org");
5 		assert(u1.scheme == "http");
6 		assert(u1.host == "example.org");
7 		assert(u1.path == "");
8 		assert(u1.port == 80);
9 		assert(u1.providedPort == 0);
10 		assert(u1.fragment == "");
11 	}
12 	{
13 		// Simple host and scheme
14 		auto u1 = parseURL("https://example.org");
15 		assert(u1.scheme == "https");
16 		assert(u1.host == "example.org");
17 		assert(u1.path == "");
18 		assert(u1.port == 443);
19 		assert(u1.providedPort == 0);
20 	}
21 	{
22 		// With path
23 		auto u1 = parseURL("https://example.org/foo/bar");
24 		assert(u1.scheme == "https");
25 		assert(u1.host == "example.org");
26 		assert(u1.path == "/foo/bar", "expected /foo/bar but got " ~ u1.path);
27 		assert(u1.port == 443);
28 		assert(u1.providedPort == 0);
29 	}
30 	{
31 		// With explicit port
32 		auto u1 = parseURL("https://example.org:1021/foo/bar");
33 		assert(u1.scheme == "https");
34 		assert(u1.host == "example.org");
35 		assert(u1.path == "/foo/bar", "expected /foo/bar but got " ~ u1.path);
36 		assert(u1.port == 1021);
37 		assert(u1.providedPort == 1021);
38 	}
39 	{
40 		// With user
41 		auto u1 = parseURL("https://bob:secret@example.org/foo/bar");
42 		assert(u1.scheme == "https");
43 		assert(u1.host == "example.org");
44 		assert(u1.path == "/foo/bar");
45 		assert(u1.port == 443);
46 		assert(u1.user == "bob");
47 		assert(u1.pass == "secret");
48 	}
49 	{
50 		// With user, URL-encoded
51 		auto u1 = parseURL("https://bob%21:secret%21%3F@example.org/foo/bar");
52 		assert(u1.scheme == "https");
53 		assert(u1.host == "example.org");
54 		assert(u1.path == "/foo/bar");
55 		assert(u1.port == 443);
56 		assert(u1.user == "bob!");
57 		assert(u1.pass == "secret!?");
58 	}
59 	{
60 		// With user and port and path
61 		auto u1 = parseURL("https://bob:secret@example.org:2210/foo/bar");
62 		assert(u1.scheme == "https");
63 		assert(u1.host == "example.org");
64 		assert(u1.path == "/foo/bar");
65 		assert(u1.port == 2210);
66 		assert(u1.user == "bob");
67 		assert(u1.pass == "secret");
68 		assert(u1.fragment == "");
69 	}
70 	{
71 		// With query string
72 		auto u1 = parseURL("https://example.org/?login=true");
73 		assert(u1.scheme == "https");
74 		assert(u1.host == "example.org");
75 		assert(u1.path == "/", "expected path: / actual path: " ~ u1.path);
76 		//assert(u1.queryParams["login"].front == "true");
77 		assert(u1.fragment == "");
78 	}
79 	{
80 		// With query string and fragment
81 		auto u1 = parseURL("https://example.org/?login=true#justkidding");
82 		assert(u1.scheme == "https");
83 		assert(u1.host == "example.org");
84 		assert(u1.path == "/", "expected path: / actual path: " ~ u1.path);
85 		//assert(u1.queryParams["login"].front == "true");
86 		assert(u1.fragment == "justkidding");
87 	}
88 	{
89 		// With URL-encoded values
90 		auto u1 = parseURL("https://example.org/%E2%98%83?%E2%9D%84=%3D#%5E");
91 		assert(u1.scheme == "https");
92 		assert(u1.host == "example.org");
93 		assert(u1.path == "/☃", "expected path: /☃ actual path: " ~ u1.path);
94 		//assert(u1.queryParams["❄"].front == "=");
95 		assert(u1.fragment == "^");
96 

Meta